import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/common/local_storage.dart';
import 'package:flutter_smart_heating/config/config.dart';
import 'package:flutter_smart_heating/ui/src/html.dart';
import 'package:flutter_smart_heating/ui/ui.dart';
import 'package:get/get.dart';

class UiPolicyPopup extends StatefulWidget {
  const UiPolicyPopup({super.key});
  static OverlayEntry? overlayEntry;
  static Function()? onAgreeFunction;
  static bool show(
    BuildContext context, {
    bool checked = false,
    Function()? onAgree,
  }) {
    if (LocalStorage.instance.getItem('agree.policy') == 'yes' && checked) {
      return true;
    }
    overlayEntry = OverlayEntry(
      maintainState: true,
      builder: (context) {
        return const UiPolicyPopup();
      },
    );
    Overlay.of(context).insert(overlayEntry!);
    onAgreeFunction = onAgree;
    return false;
  }

  static remove() async {
    overlayEntry?.remove();
    await 0.1.delay();
    onAgreeFunction?.call();
  }

  @override
  State<UiPolicyPopup> createState() => _UiPolicyPopupState();
}

class _UiPolicyPopupState extends State<UiPolicyPopup> {
  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.black54,
      child: Center(
        child: Container(
          padding: const EdgeInsets.all(
            UiTheme.spacingX,
          ),
          constraints: const BoxConstraints(maxWidth: 480, maxHeight: 520),
          width: MediaQuery.of(context).size.width - (UiTheme.spacingXX * 3),
          height: MediaQuery.of(context).size.width - (UiTheme.spacingXX * 3),
          decoration: BoxDecoration(
            color:
                UiTheme(context).matchColor(Colors.white, UiTheme.background),
            borderRadius: BorderRadius.circular(UiTheme.boxRadiusX),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text(
                '隐私协议',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
              const SizedBox(height: 10),
              Expanded(
                child: SingleChildScrollView(
                  padding: EdgeInsets.zero,
                  child: Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: UiHtml(
                      data: """
                    <p>感谢您选择xxx！我们非常重视您的个人信息和隐私保护。为了更好地保障您的个人权益，在您使用我们的产品前，请务必审慎阅读<a href="${Config.server}/plugin/page/index/detail.html?name=policy">《隐私政策》</a>和<a href="${Config.server}/plugin/page/index/detail.html?name=agreement">《用户协议》</a>内的所有条款，
                    包括但不限于:</p>
                    <p>1.我们对您的个人信息的收集/保存/使用/对外提供/保护等规则条款，以及您的用户权利等条款;</p>
                    <p>2.约定我们的限制责任、免责条款; 
                    <p>3.其他以颜色或加粗进行标识的重要条款。如您对以上协议有任何疑问，</p>
                    <p>4.您点击"同意并继续”的行为即表示您已阅读完毕并同意以上协议的全部内容。</p>
                    <p>如您同意以上协议内容，请点击"同意并继续”，开始使用我们的产品和服务!</p>
                    <p>本《协议》版权由xxx所有，xxx保留一切对本《协议》解释的权利。</p>"""
                          .replaceAll('xxx', Config.appName),
                      dense: true,
                    ),
                  ),
                ),
              ),
              const SizedBox(height: UiTheme.spacingX),
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 10),
                child: Row(
                  children: [
                    Expanded(
                      child: SizedBox(
                        height: 40,
                        child: OutlinedButton(
                          onPressed: () {
                            exit(0);
                          },
                          child: const Text('不同意退出'),
                        ),
                      ),
                    ),
                    const SizedBox(width: UiTheme.spacingX),
                    Expanded(
                      child: SizedBox(
                        height: 40,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: UiTheme.primary,
                            foregroundColor: Colors.white,
                          ),
                          onPressed: () {
                            LocalStorage.instance
                                .setItem('agree.policy', 'yes');
                            UiPolicyPopup.remove();
                          },
                          child: const Text('同意并继续'),
                        ),
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
